home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C++
/
Applications
/
PICSee Dust 1.01
/
Primary Source
/
main.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1995-11-23
|
14KB
|
579 lines
#include "main.h"
#include "InitToolBox.h"
#include "GraphicsBuffers.h"
#include "HandleAppleEvents.h"
#include "PICS_Operations.h"
#include "PICS_Merge.h"
#include "PICS_Split.h"
#include "PICSDialogs.h"
#include "PICSViewer.h"
#include "PICSPreferencesDialog.h"
#include "PICSCompositeDialog.h"
#include "AboutDialog.h"
#include "MovableModalDialogs.h"
#include "KeyUtils.h" // For CmdKeyDown()
#include "SoundUtils.h"
#include "SmartDragWindow.h"
// ---------------------------------------------------------------------------
enum {
kAppleMenuID = 128,
kFileMenuID,
kEditMenuID,
kAppleMenu_About = 1,
kFileMenu_Open = 1,
kFileMenu_Close,
kFileMenu_Save,
kFileMenu_line1,
kFileMenu_Composite,
kFileMenu_Split,
kFileMenu_Merge,
kFileMenu_line2,
kFileMenu_Prefs,
kFileMenu_line3,
kFileMenu_Quit,
kEditMenu_Undo = 1,
kEditMenu_line1,
kEditMenu_Cut,
kEditMenu_Copy,
kEditMenu_Paste,
kEditMenu_Clear,
kEditMenu_line2,
kEditMenu_SelectAll,
kEditMenu_SelectNone
};
// ---------------------------------------------------------------------------
static void QuitHandler();
static Boolean HandleEvent(EventRecord *theEvent);
static void HandleMenu(long menuCode);
static void HandleUpdate(EventRecord *theEvent) ;
static void HandleActivate(WindowPtr theWindow, Boolean activate, EventRecord *theEvent);
static void HandleOSEvent(EventRecord *theEvt);
static void AdjustMenus();
static void MyBeep();
static void MySnapProc(WindowPtr windowToDrag, short snapToDistance, Rect *snapRect);
// ---------------------------------------------------------------------------
static short gDone = false;
short gInBackground = false;
void main() {
InitToolbox();
MaxApplZone();
(void)InitGraphicsBuffers();
SetGraphicsBufferTransferMode(srcCopy+ditherCopy); // For optimum quality!
InitAppleEvents(FileDispatcher, NULL, QuitHandler);
InitMovableModalDialogs(AdjustMenus);
InitSoundUtils();
InitPrefs();
PrefsHandle prefs = LoadPrefs();
RGBColor marqueeColor;
GetPrefsMarqueeColor(prefs, &marqueeColor);
InitPICSViewer((**prefs).changeCreator, (**prefs).animationMethod, &marqueeColor);
SetPICSViewerOpenProgress((**prefs).showOpenProgress);
ReleaseResource((Handle)prefs);
MenuHandle menu;
menu = GetMenu(128);
AddResMenu(menu, 'DRVR');
InsertMenu(menu, 0);
menu = GetMenu(129);
InsertMenu(menu, 0);
menu = GetMenu(130);
InsertMenu(menu, 0);
DrawMenuBar();
AdjustMenus();
EventRecord theEvent;
Boolean eventExists;
while (!gDone) {
eventExists = WaitNextEvent(everyEvent, &theEvent, 25, NULL);
if (eventExists) {
HandleEvent(&theEvent);
}
else {
WindowPtr theWindow = FrontWindow();
if (theWindow != NULL) {
if (GetWindowKind(theWindow) == dialogKind) {
// Make text edit items blink...
short itemHit;
(void)DialogSelect(&theEvent, &theWindow, &itemHit);
HandleIdleModeless(theWindow);
}
// Send idle event only to topmost PICSViewer
theWindow = GetCurrentPICSViewer();
if (theWindow != NULL)
IdlePICSViewer(theWindow);
}
}
}
CleanupPICSViewer();
} // END main
// ---------------------------------------------------------------------------
Boolean HandleEvent(EventRecord *theEvent) {
WindowPtr theWindow;
WindowPtr topWindow = FrontWindow();
Boolean handledIt = true;
short itemHit;
// The calls to DialogSelect here serve to update and blink
// the cursor (if the dialog contains edit text items)
if (IsDialogEvent(theEvent)) {
if (topWindow != NULL && IsMovableModal(topWindow)) {
// We call the keydown handler here (not later in
// the switch statement below) so that it's invoked
// BEFORE a call to DialogSelect. We also exit
// if the key is handled by the dialog (otherwise
// the same key is acted on by the key case in
// the switch statement below - two result for
// one action -> not good!).
if (!HandleKeyDownModeless(topWindow, theEvent))
DialogSelect(theEvent, &theWindow, &itemHit);
else
return(handledIt);
}
else
(void)DialogSelect(theEvent, &theWindow, &itemHit);
}
switch(theEvent->what) {
case mouseDown:
short part;
part = FindWindow(theEvent->where, &theWindow);
switch(part) {
case inMenuBar:
AdjustMenus();
HandleMenu(MenuSelect(theEvent->where));
break;
case inSysWindow:
SystemClick(theEvent, theWindow);
break;
case inGoAway:
if (TrackGoAway(theWindow, theEvent->where)) {
if (IsPICSViewer(theWindow)) {
(void)ClosePICSViewer(theWindow);
AdjustMenus();
}
}
break;
case inDrag:
if ((theWindow != topWindow) && IsMovableModal(topWindow) &&
(!CmdKeyDown()))
MyBeep();
else
SuperSmartDragWindow(theWindow, theEvent->where, NULL, 15, MySnapProc);
break;
case inContent:
if (theWindow != topWindow) {
if (IsMovableModal(topWindow))
MyBeep();
else
SelectWindow(theWindow);
}
else {
HandleContentClickModeless(theEvent, topWindow, itemHit);
if (itemHit != 0 && IsPICSViewer(topWindow)) {
SetPort(theWindow);
ClickPICSViewer(theWindow, itemHit);
}
}
break;
case inGrow:
default:
handledIt = false;
break;
} // END switch
break;
case keyDown:
case autoKey:
char theKey = theEvent->message & charCodeMask;
if (IsMovableModal(topWindow)) {
}
else {
if (theEvent->modifiers & cmdKey) {
long menuResult;
AdjustMenus();
menuResult = MenuKey(theKey);
if (menuResult != 0) {
HandleMenu(menuResult);
}
else {
if (IsPICSViewer(topWindow))
KeyDownPICSViewer(topWindow, theKey);
}
}
else {
if (IsPICSViewer(topWindow))
KeyDownPICSViewer(topWindow, theKey);
}
}
break;
case activateEvt:
Boolean activate;
theWindow = WindowPtr(theEvent->message);
activate = (theEvent->modifiers & activeFlag) != 0;
if (IsMovableModal(theWindow))
HandleActivateModeless(theWindow, activate);
else
HandleActivate(theWindow, activate, theEvent);
AdjustMenus();
break;
case updateEvt:
HandleUpdate(theEvent);
break;
case osEvt:
HandleOSEvent(theEvent);
break;
case kHighLevelEvent:
OSErr err;
err = AEProcessAppleEvent(theEvent);
break;
default:
handledIt = true;
break;
} // END switch
return(handledIt);
} // END HandleEvent
// ---------------------------------------------------------------------------
void HandleMenu(long menuCode) {
short menuID, menuItem;
menuID = HiWord(menuCode);
menuItem = LoWord(menuCode);
if (menuID != 0)
HiliteMenu(0);
else
return;
SetCursor(&qd.arrow);
DialogPtr viewer = GetCurrentPICSViewer();
switch(menuID) {
case kAppleMenuID:
switch(menuItem) {
case kAppleMenu_About:
DoAbout();
break;
default:
Str255 deskName;
GrafPtr savePort;
GetPort(&savePort);
GetItem(GetMHandle(128), menuItem, deskName);
(void)OpenDeskAcc(deskName);
SetPort(savePort);
break;
} // END switch
break;
case kFileMenuID:
switch(menuItem) {
case kFileMenu_Open:
NewPICSViewer(NULL);
if (viewer != NULL) {
EnableItem(GetMHandle(129), 2);
}
break;
case kFileMenu_Close:
if (viewer != NULL) {
(void)ClosePICSViewer(viewer);
AdjustMenus();
}
break;
case kFileMenu_Save:
if (viewer != NULL) {
(void)SavePICSViewer(viewer);
AdjustMenus();
}
break;
case kFileMenu_Composite:
if (viewer == NULL)
SetupPICSFileComposite(NULL, NULL);
else
CompositePICSViewer(viewer);
break;
case kFileMenu_Split:
if (viewer == NULL)
SetupPICSFileSplit(NULL);
else
SplitPICSViewer(viewer);
break;
case kFileMenu_Merge:
SetupMergePICTFiles(0, NULL);
break;
case kFileMenu_Prefs:
SetupPrefsDialog();
break;
case kFileMenu_Quit:
QuitHandler();
break;
} // END switch
break;
case kEditMenuID:
switch(menuItem) {
case kEditMenu_Copy:
if (viewer != NULL) {
CopyPICSViewerFrame(viewer);
}
break;
case kEditMenu_SelectAll:
if (viewer != NULL) {
SelectEntirePICSViewerFrame(viewer, true);
}
break;
case kEditMenu_SelectNone:
if (viewer != NULL) {
SelectEntirePICSViewerFrame(viewer, false);
}
break;
}
break;
} // END switch
} // END HandleMenu
// ---------------------------------------------------------------------------
void HandleUpdate(EventRecord *theEvent) {
WindowPtr theWindow = (WindowPtr)theEvent->message;
switch(GetWindowKind(theWindow)) {
case userKind:
BeginUpdate(theWindow);
GrafPtr savePort;
GetPort(&savePort);
EraseRect(&theWindow->portRect);
SetPort(savePort);
EndUpdate(theWindow);
break;
case dialogKind:
if (IsMovableModal(theWindow)) {
HandleUpdateDialog(theWindow, theEvent, AppInBackground());
}
else if (IsPICSViewer(theWindow)) {
UpdatePICSViewer(theWindow);
}
break;
}
} // END HandleUpdate
// ---------------------------------------------------------------------------
/*
This is a quick 'n dirty method. Call this after a dialog has been
removed and before another operation takes place. Usually, if you
have a lengthy operation or another dialog shows after the previous dialog
has been dismissed, you'll get ugly white un-updated areas in your
windows where the old dialog used to be - they won't get update events
untill all of your operations are done.
This method partially fixes things. Only our windows get updated;
windows in other processes won't, since we're not sending out real
update events to all windows...
*/
void AppEmergencyUpdate() {
EventRecord theEvent;
WindowPtr targetWindow;
short oldRefNum;
// We need to save the current resource file path, since
// updating the windows might change it (a window might
// need to get a resource in the app, for example, and
// we're currently pointing to another resource fork)
oldRefNum = CurResFile();
// Update all of our windows
targetWindow = FrontWindow();
while (targetWindow != NULL) {
// Roll up own update event (kind of)
theEvent.message = (long)targetWindow;
theEvent.what = updateEvt;
theEvent.modifiers = 0;
theEvent.when = TickCount();
theEvent.where.h = theEvent.where.v = 0;
HandleEvent(&theEvent);
targetWindow = GetNextWindow(targetWindow);
}
UseResFile(oldRefNum);
} // END AppEmergencyUpdate
// ---------------------------------------------------------------------------
void HandleActivate(WindowPtr theWindow, Boolean activate, EventRecord *theEvent) {
GrafPtr savePort;
GetPort(&savePort);
SetPort(theWindow);
if (IsPICSViewer(theWindow))
ActivatePICSViewer(theWindow, activate);
SetPort(savePort);
} // END HandleActivate
// ---------------------------------------------------------------------------
void HandleOSEvent(EventRecord *theEvt) {
WindowPtr theWindow = FrontWindow();
if (((theEvt->message >> 24) & 0x0FF) == suspendResumeMessage) {
if (theEvt->message & resumeFlag != 0) {
// Resume event
gInBackground = false;
SetCursor(&qd.arrow);
if ((theWindow != NULL) && IsMovableModal(theWindow)) {
HandleActivateModeless(theWindow, true);
}
else {
if (IsPICSViewer(theWindow))
ActivatePICSViewer(theWindow, true);
}
}
else {
// Suspend event
gInBackground = true;
if ((theWindow != NULL) && IsMovableModal(theWindow)) {
HandleActivateModeless(theWindow, false);
}
else {
if (IsPICSViewer(theWindow))
ActivatePICSViewer(theWindow, false);
}
}
}
} // END HandleOSEvent
// ---------------------------------------------------------------------------
/*
AdjustMenus - called in whenever command-keys are pressed, or if the menu bar is
hit with the mouse, so the menus are adjusted only right before they're accessed.
*/
void AdjustMenus() {
WindowPtr topWindow = FrontWindow();
// Enable everything, then selectively disable as necessary
EnableItem(GetMHandle(kFileMenuID), 0);
EnableItem(GetMHandle(kEditMenuID), 0);
EnableItem(GetMHandle(kAppleMenuID), 1);
if (topWindow == NULL) {
DisableItem(GetMHandle(kFileMenuID), kFileMenu_Close);
DisableItem(GetMHandle(kFileMenuID), kFileMenu_Save);
DisableItem(GetMHandle(kEditMenuID), 0);
}
else if (IsMovableModal(topWindow)) {
DisableItem(GetMHandle(kAppleMenuID), kAppleMenu_About);
DisableItem(GetMHandle(kFileMenuID), 0);
DisableItem(GetMHandle(kEditMenuID), 0);
}
else if (IsPICSViewer(topWindow)) {
EnableItem(GetMHandle(kFileMenuID), kFileMenu_Open);
EnableItem(GetMHandle(kFileMenuID), kFileMenu_Close);
if (IsPICSViewerModified(topWindow)) {
EnableItem(GetMHandle(kFileMenuID), kFileMenu_Save);
}
else {
DisableItem(GetMHandle(kFileMenuID), kFileMenu_Save);
}
EnableItem(GetMHandle(kEditMenuID), kEditMenu_Copy);
EnableItem(GetMHandle(kEditMenuID), kEditMenu_SelectAll);
EnableItem(GetMHandle(kEditMenuID), kEditMenu_SelectNone);
}
DrawMenuBar();
} // END AdjustMenus
// ---------------------------------------------------------------------------
void QuitHandler() {
DialogPtr viewer;
viewer = NULL;
do {
viewer = GetCurrentPICSViewer();
if (viewer != NULL) {
if (!ClosePICSViewer(viewer)) {
return;
}
}
} while (viewer != NULL);
gDone = true;
} // END QuitHandler
// ---------------------------------------------------------------------------
Boolean AppInBackground() {
return(gInBackground);
}
// ---------------------------------------------------------------------------
void MyBeep() {
Handle beepSnd = GetResource('SND!', 128);
if (beepSnd != NULL && CreateSndChannel(0) == noErr) {
PlayAsynch(beepSnd, 0);
WaitTillSndDone(0);
HUnlock((Handle)beepSnd);
ReleaseResource(beepSnd);
DisposeSndChannel(0);
}
} // END MyBeep
// ---------------------------------------------------------------------------
void MySnapProc(WindowPtr windowToDrag, short snapToDistance, Rect *snapRect) {
WindowSnapProc(windowToDrag, snapToDistance, snapRect);
MonitorSnapProc(windowToDrag, snapToDistance, snapRect);
} // END MySnapProc